package com.practice.J2E.annotation;

import cn.hutool.core.util.StrUtil;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * @author kayn
 * @create 2023-10-07 上午 09:40
 */
public interface SplitHandler {

    /**
     * 切割字符串
     * @param fieldName
     * @return
     */
    default List<String> getStrList(String fieldName) {

        try {

            //获取字段对象
            Field field = this.getClass().getDeclaredField(fieldName);

            field.setAccessible(true);

            //获取字段对象的值
            String str = (String) field.get(this);

            //获取注解对象
            SplitMethod annotation = field.getAnnotation(SplitMethod.class);

            //获取注解指定的分隔符
            String separator = annotation.separator();

            //切割字段串
            return StrUtil.split(str, separator);

        } catch (Exception e) {

            e.printStackTrace();

        }

        return new ArrayList<>();
    }
}
